﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace MToolStudio.Render
{
    public class UIUtility
    {
        //private static int stateNameCounter;
        //private static int actionNameCounter;

        public static double DistanceSqrBetween(Point p, Point q)
        {
            double dx = p.X - q.X;
            double dy = p.Y - q.Y;
            return (dx * dx + dy * dy);
        }

        public static double DistanceBetween(Point p, Point q)
        {
            return Math.Sqrt(DistanceSqrBetween(p, q));
        }

        public static Point PMinusP(Point p, Point q)
        {
            return new Point((p.X - q.X) / 2, (p.Y - q.Y) / 2);
        }

        public static Point PAddP(Point p, Point q)
        {
            return new Point((p.X + q.X) / 2, (p.Y + q.Y) / 2);
        }

        //public static string GenStateName()
        //{
        //    return "state_" + (stateNameCounter++);
        //}

        //public static string GenActionName()
        //{
        //    return "action_" + (actionNameCounter++);
        //}

        private const double curveHeightLineDistRatio = 0.18;
        private const double selfCurveRadius = 60;
        private const int selfCurveSpanDegrees = 25;        
        //private static Random _random = new Random();

        public static Point[] GetCurvedPoints(Point from, Point to, int ithSelfCurve)
        {
            //double d = DistanceBetween(from, to);
            List<Point> ptList = new List<Point>();
            ptList.Add(from);
            if (from == to)
            {
                int deg = ithSelfCurve * selfCurveSpanDegrees * 3;
                for (int ithP = 0; ithP < 3; ++ithP)
                {
                    double rad = (double)deg / 180 * Math.PI;
                    double radiusRatio = ithP == 1 ? 1 : 0.8;
                    ptList.Add(new Point(from.X + (int)(selfCurveRadius * radiusRatio * Math.Sin(rad))
                        , from.Y + (int)(selfCurveRadius * radiusRatio * Math.Cos(rad))));
                    deg += selfCurveSpanDegrees;
                }
            }
            else
            {
                Point mid = PAddP(from, to);
                double dx = to.X - from.X;
                double dy = to.Y - from.Y;
                double cx = -dy * curveHeightLineDistRatio;
                double cy = dx * curveHeightLineDistRatio;
                ptList.Add(new Point((int)(mid.X - cx), (int)(mid.Y - cy)));
            }
            ptList.Add(to);
            return ptList.ToArray();
        }

        public static double DistanceBetween(PointF p, PointF q)
        {
            double dx = p.X - q.X;
            double dy = p.Y - q.Y;
            return (dx * dx + dy * dy);
        }
    }
}
